package FCRendering.sw;

import FCRendering.FCRMaster;
import FCRendering.NpcBundle;
import VWorldGameStudio.FCAction;
import socket.FCRSocketClient;
import socket.FCRSocketServer;
import utils.FCDataSingles;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;
import java.util.Timer;

import static FCRendering.FCRMaster.*;
import static FCRendering.FCRMaster.fcv;

public class FCRLoadData extends Thread {
    @Override
    public void run() {
        FCRMaster.fcv.setLoadNum(0);
        FCRMaster.fcv.setGameAlldataNum(1 + FCRMaster.blockLib.getBlockAllNum() + FCRMaster.eventLib.getEventAllNum() + FCRMaster.actionLib.getActionAllNum()
                + FCRMaster.monLib.getMonsterAllNum() + FCRMaster.animationLib.getAnimationAllNum() + FCRMaster.itemLib.getItemAllNum()
                + FCRMaster.plotLib.getPlotAllNum() + FCRMaster.tankLib.getTankAllNum() + FCRMaster.mapLib.getMapAllNum()+ mapAnimationLibrary.getPreCountNum());
        //TODO 如果是用了存档的，为了本机的hostId唯一相同性，要重设hostID
        if (FCRMaster.fcv.getSaveMouseClick() > -1) {
            FCRMaster.saveList.preInit(FCRMaster.fcv.getSaveMouseClick());
        }


        //数据加载
        FCRMaster.blockLib.loadBlockData();
        FCRMaster.eventLib.loadEventData();
        FCRMaster.actionLib.loadActionData();
        FCRMaster.monLib.loadMonsterData();
        FCRMaster.animationLib.loadAnimationData();
        FCRMaster.itemLib.loadItemData();
        FCRMaster.plotLib.loadPlotData();
        FCRMaster.tankLib.loadTankData();
        FCRMaster.mapLib.loadMapData();
        FCRMaster.mapAnimationLibrary.loadAimationData();
        boolean loadALl = true;
        //存档加载
        if (FCRMaster.fcv.getSaveMouseClick() > -1) {
            loadALl = FCRMaster.saveList.unloadsaveinfo(FCRMaster.player1, FCRMaster.gameSave, FCRMaster.gamedate, FCRMaster.tankLib,
                    FCRMaster.fcv.getSaveMouseClick());
        }else {
            //必须滴
            FCRMaster.itemLib.setExtraItemCloneImpl(FCRMaster.gameSave);
        }
        System.out.println("load init loadALl:"+loadALl+" "+FCRMaster.fcv.getSaveMouseClick()+
                " "+FCRMaster.monLib.getMonsterNewID()+":"+FCRMaster.monLib.getMonsterAllNum()
                +","+FCRMaster.animationLib.getAnimationNum()+":"+FCRMaster.animationLib.getAnimationAllNum());
        //TODO 这里做一下还原操作
        if(loadALl) {
            FCRMaster.fcv.setShowWhosBagItem(FCRMaster.player1.getName());
            if (FCRMaster.fcv.isSever()) {
                FCRSocketServer.getIn().initServer();
            } else if (FCRMaster.fcv.isClient()) {
                FCRSocketClient.getIn().initClient();
            }
            HashMap<String, String> hashnpc = gameSave.getCaseiteminfo();
            Set<String> hashset = hashnpc.keySet();
            for (String as : hashset) {
                eventLib.setCaseInfo(as, hashnpc.get(as));
            }
            hashnpc = mapLib.getNPCOfName(player1.getMapname());
            hashset = hashnpc.keySet();
            for (String opop : hashset) {
                fcv.getNpcName().add(hashnpc.get(opop));
                int dir = actionLib.getActionOfName(charLib.getCharacterOfName(hashnpc.get(opop)).getCharacterActionID())
                        .getFace();
                int my = Integer.parseInt(opop.split("\\*")[1]);
                int mx = Integer.parseInt(opop.split("\\*")[0]);
                FCAction act = actionLib.getActionOfName(charLib.getCharacterOfName(hashnpc.get(opop)).getCharacterActionID());
                fcv.getNpcBundleHashMap().put(hashnpc.get(opop), new NpcBundle(hashnpc.get(opop), mx, my, dir, act.getType()));
            }
        }
        //TODO 必须必须把这个放到最后，
        FCRMaster.fcv.setSaveUploadIsOk(loadALl?1:0);
        System.out.println("loadALl finish:"+fcv.getLoadFileNum());
        //时间开始计时
        new Timer().schedule(new FCRDateTimeUpdate(), new Date(), 1000);
    }
}
